//
//  EnterViewController.swift
//  xlogin
//
//  Created by bin hu on 2025/6/19.
//

import UIKit

class EnterViewController: UIViewController {

    private lazy var tableView:UITableView = {
        let tableV = UITableView(frame: CGRect(x: 0, y: 100, width: view.bounds.size.width, height: view.bounds.size.height - 100), style: .grouped)
        tableV.backgroundColor = .clear
        tableV.delegate = self
        tableV.dataSource = self
        tableV.bounces = false
//        tableV.separatorStyle = .none
        tableV.showsVerticalScrollIndicator = false
        if #available(iOS 15.0, *) {
            tableV.sectionHeaderTopPadding = 0
        } else {
            // Fallback on earlier versions
        }
        tableV.contentInsetAdjustmentBehavior = .never
        tableV.register(UITableViewCell.self, forCellReuseIdentifier: NSStringFromClass(UITableViewCell.self))
        view.addSubview(tableV)
        return tableV
    }()
    
    let titles = ["设置","修改","验证"]

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        view.backgroundColor = .white
        tableView.reloadData()
    }

}

extension EnterViewController:UITableViewDelegate, UITableViewDataSource {

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return titles.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: NSStringFromClass(UITableViewCell.self))!
        cell.textLabel?.text = titles[indexPath.row]
        return cell
    }
    
    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
        return 44.0
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        let title = titles[indexPath.row]
        switch title {
        case "设置":
            let vc = ExampleViewController(config: NightBlueConfig(), type: .setup)
            self.navigationController?.pushViewController(vc, animated: true)
        case "修改":
            if PasswordManager.currentPassword() == nil {
                let alert = UIAlertController(title: nil, message: "当前没有密码，请先设置", preferredStyle: .alert)
                let confirm = UIAlertAction(title: "好的", style: .cancel, handler: nil)
                alert.addAction(confirm)
                present(alert, animated: true, completion: nil)
            }else {
                let vc = ExampleViewController(config:  NightBlueConfig(), type: .modify)
                self.navigationController?.pushViewController(vc, animated: true)
            }
        case "验证":
            if PasswordManager.currentPassword() == nil {
                let alert = UIAlertController(title: nil, message: "当前没有密码，请先设置", preferredStyle: .alert)
                let confirm = UIAlertAction(title: "好的", style: .cancel, handler: nil)
                alert.addAction(confirm)
                present(alert, animated: true, completion: nil)
            }else {
                let vc = ExampleViewController(config:  NightBlueConfig(), type: .vertify)
                self.navigationController?.pushViewController(vc, animated: true)
            }
        default:
            break
        }
    }
}

